﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OrderService;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;

namespace _25_3_29
{
    public partial class Form3 : Form
    {
        OrderService _service;
        private BindingList<Order> _orders;
        string s;
        public Form3(OrderService orderservice)
        {
            InitializeComponent();
            _service = orderservice;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //其他查询逻辑在查询实现
            if (comboBox1.SelectedIndex == 0)
            {
                int _id = 0;
                if (int.TryParse(s, out int id))
                {
                    _id = id;
                }
                else
                {
                    MessageBox.Show("请输入有效的数字作为订单ID");
                    textBox1.Text = ""; // 清空无效输入
                    textBox1.Focus();
                }

                var parameters = new OrderQueryParameters
                {
                    OrderId=_id // 明确指定查询字段
                };
                _orders = new BindingList<Order>(_service.QueryOrders(parameters));
                if (_orders == null)
                {
                    MessageBox.Show("未找到指定ID的订单");
                    return;
                }
                else
                {
                    dataGridView1.DataSource = _orders;
                }

            }
            else if (comboBox1.SelectedIndex == 1)
            {
                if (IsValidCustomerName(s))
                {
                    var parameters = new OrderQueryParameters
                    {
                        Customer = s 
                    };
                    _orders = new BindingList<Order>(_service.QueryOrders(parameters));
                    if (_orders == null)
                    {
                        MessageBox.Show("未找到指定ID的订单");
                        return;
                    }
                    else
                    {
                        dataGridView1.DataSource = _orders;
                    }

                }
                else
                {
                    MessageBox.Show("请输入有效的客户名称（不能为空或纯数字）");
                    textBox1.Text = ""; // 清空无效输入
                    textBox1.Focus();
                }

            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text))
            {
                return; // 如果为空则不处理
            }
            s = textBox1.Text;

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private bool IsValidCustomerName(string name)
        {
            // 1. 不能为空或纯空格
            if (string.IsNullOrWhiteSpace(name))
                return false;

            // 2. 不能是纯数字
            if (name.All(char.IsDigit))
                return false;

            // 3. 不能包含特殊字符等
            if (name.Any(c => !char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c)))
                return false;

            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (_orders == null || !_orders.Any())
            {
                MessageBox.Show("没有可删除的订单");
                return;
            }

            var result = MessageBox.Show(
                "确定要删除所有订单吗？此操作不可恢复！",
                "警告",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (result != DialogResult.Yes) return;


            var orderIdsToDelete = _orders.Select(o => o.OrderId).ToList();

            try
            {

                foreach (int id in orderIdsToDelete)
                {
                    _service.RemoveOrder(id);
                }


                _orders.Clear();
                MessageBox.Show($"已删除 {orderIdsToDelete.Count} 个订单", "完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"删除失败: {ex.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要取消删除吗？", "确认",
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                _orders.Clear();
                this.Close();
            }
        }
    }
}
